﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace IcicleFramework.Renderables
{
    /// <summary>
    /// An Image class for an 'image' that can be composed of multiple child images.
    /// </summary>
    public class CompositeImage : TexturedRenderable
    {

        private Dictionary<string, IRenderable> images;
        
        public Dictionary<string, IRenderable> Images { get { return images; } }

        public CompositeImage()
        {
            this.images = new Dictionary<string, IRenderable>();
        }

        public CompositeImage(Dictionary<string, IRenderable> images)
        {
            this.images = images;
        }

        public CompositeImage(CompositeImage old)
        {
            this.images = new Dictionary<string, IRenderable>();

            foreach (KeyValuePair<string, IRenderable> pair in old.images)
                this.images.Add(pair.Key, (IRenderable) pair.Value.DeepClone());
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            foreach (IRenderable image in images.Values)
                image.Draw(spriteBatch);
        }

        public override void Load(ContentManager content)
        {
            foreach (IRenderable image in images.Values)
                image.Load(content);
        }

        public void AddImage(IRenderable newImage, string key)
        {
            if (!images.ContainsKey(key))
                images.Add(key, newImage);
        }

        public override IRenderable DeepClone()
        {
            return new CompositeImage(this);
        }

        public override void Update(GameTime gameTime)
        {
            foreach (IRenderable image in images.Values)
                image.Update(gameTime);
        }
    }
}
